package com.hclc.util.sftp;

import lombok.extern.slf4j.Slf4j;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * MavenModel
 *
 * @ClassName MavenModelResourceUtil
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 15:23 2021/1/6
 * @version: 1.0
 */
@Slf4j
public class MavenModelResourceUtil {
    /**
     * pom名称
     */
    private static final String POM_NAME = "pom.xml";

    /**
     * 工程根目录
     */
    private static final String PROJECT_ROOT_DIRECTORY = "user.dir";

    /**
     * pom资源缓存
     */
    private static Map<String, Model> MODEL_CACHE_MAP = new HashMap<>();

    /**
     * 根目录pom key
     */
    private static final String PROJECT_ROOT_MODEL_KEY = "PROJECT_ROOT_MODEL_KEY";

    /**
     * 获取指定路径pom资源
     *
     * @param
     * @return org.apache.maven.model.Model
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:25
     */
    public static Model getResourceByPath(String pomPath) {
        MavenXpp3Reader reader = new MavenXpp3Reader();
        Model model = null;
        try {
            model = reader.read(new FileReader(pomPath));
        } catch (IOException e) {
            log.error("读取pom文件异常", e);
        } catch (XmlPullParserException e) {
            log.error("pom文件转换异常", e);
        }
        return model;
    }

    /**
     * 获取指定model资源
     *
     * @param key
     * @return org.apache.maven.model.Model
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:49
     */
    public static Model get(String key) {
        return MODEL_CACHE_MAP.get(key);
    }

    /**
     * 添加指定model资源
     *
     * @param key
     * @return void
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:48
     */
    public static void set(String key, Model model) {
        MODEL_CACHE_MAP.put(key, model);
    }

    /**
     * 删除指定model资源
     *
     * @param key
     * @return void
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:48
     */
    public static void remove(String key) {
        if (MODEL_CACHE_MAP.containsKey(key)) {
            MODEL_CACHE_MAP.remove(key);
        }
    }

    /**
     * 获取根目录pom资源
     *
     * @return org.apache.maven.model.Model
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:33
     */
    public static Model getRootResource() {
        return getRootResource(false);
    }

    /**
     * 获取根目录pom资源
     *
     * @param refreshCache 是否刷新缓存
     * @return org.apache.maven.model.Model
     * @author yurj
     * @version 1.0
     * @date 2021/1/6 15:33
     */
    public static Model getRootResource(boolean refreshCache) {
        // 根目录pom路径
        String projectPomPath = System.getProperty(PROJECT_ROOT_DIRECTORY) + File.separator + POM_NAME;
        // 刷新缓存资源
        if (refreshCache) {
            Model model = getResourceByPath(projectPomPath);
            MODEL_CACHE_MAP.put(PROJECT_ROOT_MODEL_KEY, model);
            return model;
        }
        // 直接获取缓存
        Model model = MODEL_CACHE_MAP.get(PROJECT_ROOT_MODEL_KEY);
        if (Objects.nonNull(model)) {
            return model;
        }
        return getRootResource(true);
    }
}
